package com.example.test_load_pic_rough.adapter;

import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_load_pic_rough.R;
import com.example.test_load_pic_rough.bean.ItemInfo;

import java.util.ArrayList;

public class ChromometerAdapter extends RecyclerView.Adapter<ChromometerAdapter.ChromometerViewHolder>{

    private ArrayList<ItemInfo> lists = new ArrayList<>();

    public ChromometerAdapter(ArrayList<ItemInfo> lists) {
        this.lists = lists;
    }

    public void addItem(ItemInfo itemInfo){
        lists.add(itemInfo);
        notifyItemInserted(lists.size()-1);
    }

    @NonNull
    @Override
    public ChromometerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.chromometer_item, parent, false);
        return new ChromometerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ChromometerViewHolder holder, int position) {
        holder.tv.setText(lists.get(position).getCode());
        holder.time.setBase(SystemClock.elapsedRealtime() - (System.currentTimeMillis()-lists.get(position).getCreateTime()));
        holder.time.start();
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public int getItemViewType(int position) {
        Log.e("sss", "getItemViewType: " );
        return super.getItemViewType(position);
    }

    class ChromometerViewHolder extends RecyclerView.ViewHolder{
        private TextView tv ;
        private Chronometer time ;
        public ChromometerViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.code);
            time = itemView.findViewById(R.id.time);
        }
    }

}

